var config = {
    publicUrl: "http://localhost:3000/catetype/",
    cateUrl: "http://localhost:3000/cate/",
    config_create: '',
    config_update: ''
};

//初始化
$('#tools').datagrid({
    url: `${config.publicUrl}list`,
    method: "post",
    view: transposedview,
    pagination: true,
    singleSelect: true,//只选择一个
    columns: [[
        {
            field: 'name', title: '目录', width: 100, align: 'center',
            formatter: function (value, row, index) {
                return `<a id="btn" href="javascript:void(0);" class="easyui-linkbutton" onclick="Search('${row._id}')">${row.name}</a>`
            }
        }
    ]],
    toolbar: [{
        text: "增加目录",
        iconCls: 'icon-add',
        handler: function () {
            $('#form').form('clear');
            $('#dialog').dialog("open");
        }
    },
    {
        text: "删除目录",
        iconCls: 'icon-remove',
        handler: function () {
            //删除数据
            var select = $("#tools").datagrid("getSelected");
            if (select != null) {
                $.messager.confirm('Confirm', '您确定要删除该目录数据吗?', function (r) {
                    if (r) {
                        //删除目录
                        $.ajax({
                            url: `${config.publicUrl}delete/${select._id}`,
                            method: "delete",
                        }).then(res => {
                            //删除树
                        $('#tree').tree({
                            url: `${config.cateUrl}delete`,
                            method: "delete",
                            queryParams: { id: select._id }
                        });
                            $('#tools').datagrid('reload');
                        });

                        $("#tree").html('<p style="text-align: center;font-size:20px">请选择分类目录</p>');
                    }
                });
            } else {
                $.messager.alert('Warning', '您没有选中数据');
            }
        }
    },
    {
        text: "编辑目录",
        iconCls: 'icon-edit',
        handler: function () {
            var select = $("#tools").datagrid("getSelected");
            if (select != null) {
                $('input[name=tab]').val(select._id);
                $.ajax({
                    url: `${config.publicUrl}/${select._id}`,
                    method: "get",
                }).then(res => {
                    $('#form').form('load', res);
                });
                $('#dialog').dialog("open");
            } else {
                $.messager.alert('Warning', '您没有选中数据');
            }
        }
    },
    {
        text: '查询数据',
        iconCls: 'icon-search',
        handler: function () {
            $("#tools").datagrid({
                url: `${config.publicUrl}list`,
                method: "post",
                queryParams: {
                    search: $("input[name=search]").val(),
                }
            });
        }
    }],
    //数据加载成功以后渲染
    onLoadSuccess: function () {
        $(".datagrid-cell").css({ "height": "60px", "line-height": "60px" })
        $("#searchtext").remove();
        var saerch = '<div id="searchtext"><input class="easyui-textbox" name="search" style="width:100px"></div> '
        $(".datagrid-toolbar>table td:eq(3)").before(saerch);
        $.parser.parse();
    }
});

//数据显示层
$('#panels').panel({
    // fit:true,
    title: '数据显示',
    tools: [{
        iconCls: 'icon-add',
        handler: function () {
            $('#form_dom').form('clear');
            var add = $('#tree').tree('getSelected');
            if (add != null) {
                $('#dom_dialog').dialog("open");
                var add_arr = {
                    parentId: add._id,
                    type: add.type
                }
                $('input[name=off]').val("");
                config.config_create = add_arr;
            } else {
                $.messager.alert('Warning', '请选中节点');
            }
        }
    }, {
        iconCls: 'icon-edit',
        handler: function () {
            // alert('edit')
            var edit = $('#tree').tree('getSelected');
            if (edit != null) {
                $.ajax({
                    url: `${config.cateUrl}ls/${edit._id}`,
                    method: "get"
                }).then(res => {
                    $('#form_dom').form('load', res);
                });
                $('input[name=off]').val(edit._id);
                $('#dom_dialog').dialog("open");
                var edit_arr = {
                    _id: edit._id,
                    type: edit.type
                }
                config.config_update = edit_arr;
            } else {
                $.messager.alert('Warning', '请选中节点');
            }
        }
    }, {
        iconCls: 'icon-remove',
        handler: function () {
            // $('#tree').tree('getSelected','getRoots');
            var remove = $('#tree').tree('getSelected');
            if (remove != null) {
        $.messager.confirm('Confirm', '您确定要删除选中数据吗?', function (r) {
            if (r) {
                let ids = [];
                let arr = $('#tree').tree('getSelected', 'getRoots');
                if (arr._id != undefined) {
                    ids.push(arr._id);
                }
                //先判断是否有子目录
                if (arr.children != undefined) {
                    xh(arr, ids);
                }

                //循环遍历所有子集
                function xh(child, arrs) {
                    for (let i = 0; i < child.children.length; i++) {
                        arrs.push(child.children[i]._id);
                        if (child.children[i].children != undefined) {
                            // console.log(object);
                            xh(child.children[i], arrs);
                        }
                    }
                }
                // console.log(ids);
                $('#tree').tree({
                    url: `${config.cateUrl}removes`,
                    method: "delete",
                    animate: true,
                    queryParams: { ids: ids.toString(), type: remove.type }
                });
            }
        });
            } else {
                $.messager.alert('Warning', '请选中节点');
            }
        }
    }]
});



//配置增删改查表单信息
$('#dialog').dialog({
    title: '数据信息',
    width: 400,
    height: 200,
    closed: true,
    cache: false,
    modal: true
});

//配置节点增改表单信息
$('#dom_dialog').dialog({
    title: '节点信息',
    width: 400,
    height: 200,
    closed: true,
    cache: false,
    modal: true
});

//清除表单数据
function clearForm() {
    $('#form').form('clear');
}
//清除表单数据
function clear_dom_Form() {
    $('#form_dom').form('clear');
}

//提交表单数据
function submitForm() {
    $('#form').form('submit', {
        onSubmit: function () {
            if ($(this).form('enableValidation').form('validate')) {
                if ($('input[name=tab]').val() == "") {
                    const data = $('#form').serializeJSON();
                    $.ajax({
                        url: `${config.publicUrl}create`,
                        method: "post",
                        data
                    }).then(res => {
                        $('#dialog').dialog("close");
                        $('#tools').datagrid("reload");
                    })
                } else {
                    const data = $('#form').serializeJSON();
                    $.ajax({
                        url: `${config.publicUrl}update/${$('input[name=tab]').val()}`,
                        method: "put",
                        data: data,
                    }).then(res => {
                        // console.log(res);
                        $('#dialog').dialog("close");
                        $('#tools').datagrid('reload');
                    })
                }
            }
        }
    });
}

//提交节点表单数据
function submit_dom_Form() {
    $('#form_dom').form('submit', {
        onSubmit: function () {
            if ($(this).form('enableValidation').form('validate')) {
                if ($('input[name=off]').val().length <= 0) {
                    const data = $('#form_dom').serializeJSON();
                    config.config_create.text = data.text;
                    $('#tree').tree({
                        url: `${config.cateUrl}create`,
                        method: "post",
                        animate: true,
                        queryParams: config.config_create
                    });
                    $('#dom_dialog').dialog("close");
                } else {
                    const data = $('#form_dom').serializeJSON();
                    config.config_update.text = data.text;
                    $('#tree').tree({
                        url: `${config.cateUrl}update`,
                        method: "put",
                        animate: true,
                        queryParams: config.config_update
                    });
                    $('#dom_dialog').dialog("close");
                }
            }
        }
    });
}


//查询树
function Search(id) {
    var select = $("#tools").datagrid("getSelected");
    if (select != null) {
        $('#tree').tree({
            url: `${config.cateUrl}list`,
            method: "post",
            animate: true,
            queryParams: { "type": id, "text": $("#tools").datagrid("getSelected").name }
        });
    } else {
        $.messager.alert('Warning', '对不起，您没有选中数据');
    }
    return false;
}
